package com.qingxin.park.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.Material;
import com.qingxin.park.entity.MaterialType;
import com.qingxin.park.service.MaterialService;
import com.qingxin.park.service.MaterialTypeService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;


@Controller
@RequestMapping("/material")
public class MaterialAction extends BaseAction {

	@Autowired
	private MaterialTypeService materialTypeService;
	@Autowired
	private MaterialService materialService;
	/**
	 * 获取物资数据
	 * @param parkid
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/getMaterialData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getMaterialData(String parkid){
		List<TreeNode> list = materialTypeService.getUITreeData(parkid, true);
		for(TreeNode node : list){
			node.setChildren(materialService.getUITreeData(parkid, node.getId(), false));
		}
		return list;
	}
	
	
	@RequestMapping(value="/getSelectData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getSelectData(String parkid, Integer typeKind){
		List<Map<String, Object>> list = new ArrayList<>();
		List<MaterialType> typeList = materialTypeService.listByTypeKind(typeKind);
		if(typeList != null){
			for(MaterialType type : typeList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", type.getTypeid());
				map.put("name", type.getTypeName());
				List<Map<String, Object>> children = new ArrayList<>();
				List<Material> materialList = materialService.listByType(parkid, type.getTypeid());
				if(materialList != null && materialList.size()>0){
					for(Material material : materialList){
						Map<String, Object> cmap = new HashMap<String, Object>();
						cmap.put("id", material.getMaterialid());
						cmap.put("name", material.getMaterialName());
						children.add(cmap);
					}
					
					map.put("children", children);
					
					list.add(map);
				}
				
			}
		}
		return list;
	}
	
	
	
	/**
	 * 加载树形数据
	 * @param pdatatype
	 * @param pid
	 * @return
	 
	@RequestMapping(value="/materialtree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object cropstree(String pdatatype, String pid){
		if(StringUtils.isEmpty(pid)){
			List<TreeNode> list = new ArrayList<TreeNode>();
			list.add(new TreeNode("0", "物资管理", "ROOT", null, true));
			return list;
			
		}else{
			return materialTypeService.getUITreeData(pid, false);
		}
	}*/
	
	
	
	/**
	 * 类型列表分页数据
	 * @param leftdata
	 * @param datatype
	 * @param materialName
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object typelistdata(String leftdata, String materialid, String materialName, PageParam pageParam, HttpServletRequest request){
		String typeid = null;
		if(!"0".equals(leftdata)){
			typeid = leftdata;
		}
		//String parkid = null;
		Pager pager = materialService.list(materialid, typeid, materialName, pageParam, getLoginUser(request));
		return pager;	
	}
	
	
	
	
	
	/**
	 * 添加物资信息
	 * @param model
	 * @param leftdata
	 * @param datatype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, String datatype, HttpServletRequest request){
		if(!"TYPE".equals(datatype) || StringUtils.isEmpty(leftdata)){
			model.put("errormessage", "参数错误");
			return "sys/error/error";
		}
		model.put("typeid", leftdata);
		
		MaterialType materialType = materialTypeService.get(leftdata);
		model.put("materialType", materialType);
		
		return "trace/material/material_add";
	}
	
	
	/**
	 * 保存物资信息
	 * @param material
	 * @param result
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid Material material, BindingResult result, String typeid, String parkid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = materialService.add(material, typeid, parkid, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改物资信息
	 * @param model
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String materialid, HttpServletRequest request){
		Material material = materialService.get(materialid);
		model.put("material", material);
		MaterialType materialType = materialTypeService.get(material.getMaterialType().getTypeid());
		model.put("materialType", materialType);
		
		return "trace/material/material_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param material
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid Material material, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = materialService.update(material, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看物资详情
	 * @param model
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do" , method=RequestMethod.GET)
	public String view(ModelMap model, String materialid, HttpServletRequest request){
		com.qingxin.park.entity.Material material = materialService.get(materialid);
		model.put("material", material);
		
		MaterialType materialType = materialTypeService.get(material.getMaterialType().getTypeid());
		model.put("materialType", materialType);
		
		return "trace/material/material_view";
	}
	
	
	
	/**
	 * 删除农作物信息
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] materialid, HttpServletRequest request){
		if(materialid==null || materialid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return materialService.delete(materialid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断物资名称是否存在
	 * @param blockName
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String materialName, String materialid, String typeid){
		boolean existed = materialService.isNameDuplicate(materialName, materialid, typeid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断物资编号是否存在
	 * @param blockCode
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isCodeDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isCodeDuplicate(String materialCode, String materialid, String typeid){
		boolean existed = materialService.isCodeDuplicate(materialCode, materialid, typeid);
		return existed ? "1":"0";
	}
}
